﻿using System;
using System.Configuration;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;

namespace good1.Web.Helpers
{
    public static class HtmlHelperExtensions 
    {

        const string Script = "<script type=\"text/javascript\" src=\"{0}\"></script>";
        /// <summary>
        /// Version 3 of google maps would be nice as would remove need for key.  However, there are breaking
        /// changes in upgrade as of 2009-07-24 for our code
        /// </summary>
        /// <returns></returns>
        public static string GetGoogleJavaScript(this HtmlHelper html)
        {
            return string.Format(Script, "http://www.google.com/jsapi?&sensor=false&key=" + ConfigurationManager.AppSettings["GoogleMapsKey"]);
        }

        public static void RedirectPermanently(this HttpResponse response, string url)
        {
            response.Buffer = true;
            response.Status = "301 Redirect";
            response.AddHeader("Location", url);
            response.End();

        }

        public static string GetGoogleMapsJavaScript(this HtmlHelper html)
        {
            return string.Format(Script, "http://maps.google.com/maps?file=api&amp;v=2&amp;key=" + ConfigurationManager.AppSettings["GoogleMapsKey"] + "&sensor=false");
        }

        public static string GetScript(this HtmlHelper html, string fileName)
        {
            return string.Format(Script, "http://d1rrjg1ct2t0vp.cloudfront.net/" + fileName);
        }

        
            public static string Label(this HtmlHelper helper, string target, string text)
            {
                return String.Format("<label for='{0}'>{1}</label>", target, text);
            }


            public static string KeyValuePairDisplay(this HtmlHelper helper, string key, string value)
            {
                return String.Format("<div class=\"value\">{1}</div><div class=\"key\">{0}:</div>", key, value);
            }

        public static string GetCss(this HtmlHelper html, string stylesheetName)
        {
            return GetCss(html, stylesheetName, "1");
        }

        public static string GetCss(this HtmlHelper html, string stylesheetName, string version)

        {
            //version helps flushing cached local versions
            return
                string.Format(
                    "<link rel=\"stylesheet\" href=\"/content/style/{0}.css?v={1}\" type=\"text/css\" media=\"screen\" title=\"no title\" charset=\"utf-8\" />", stylesheetName, version);

        }



        public static string ProjectThumbNailLink(this HtmlHelper helper, int projectId, string projectTitle, string photoUrl, string imageClass, string thumbnailUrl)
        {
            if (string.IsNullOrEmpty(thumbnailUrl)) thumbnailUrl = photoUrl;
            return ProjectThumbNailLink(helper, projectId, projectTitle, thumbnailUrl, imageClass);
        }
      
       public static string ProjectThumbNailLink(this HtmlHelper helper, int projectId, string projectTitle, string photoUrl, string imageClass)
        {
           if (string.IsNullOrEmpty(imageClass)) imageClass = "project-photo";
           
           return ImageLink(helper, 
                            new {action = "Show", controller = "projects", id = projectId, title = projectTitle},
                            "full project details of" + projectTitle, photoUrl, projectId, imageClass );

          
        }

       
        //Html.Substitution could be very interesting but going for caching of more particulars instead.
       //https://msmvps.com/blogs/luisabreu/archive/2009/03/25/the-mvc-framework-pay-one-take-several.aspx
       

        public static string ImageLink(this HtmlHelper helper, object routeValues, string title, string imageUrl, int id, string imageClass)
        {
            UrlHelper url = new UrlHelper(helper.ViewContext.RequestContext);
            StringBuilder thumb = new StringBuilder();
            thumb.Append("<div class=");
            thumb.Append((char)(34));
            thumb.Append(imageClass);
            thumb.Append("-div");
            thumb.Append((char)(34));
            thumb.Append(">");
            thumb.Append("<a href=");
            thumb.Append((char)(34));
            thumb.Append(url.RouteUrl(routeValues));
            thumb.Append((char)(34));
            thumb.Append(" title=\"");
            thumb.Append(helper.Encode(title));
            thumb.Append((char)(34));
            thumb.Append(">");
            thumb.Append("<img src=");
            thumb.Append((char)(34));
            thumb.Append(helper.Encode(imageUrl));
            thumb.Append((char)(34));
            thumb.Append(" id=\"img_");
            thumb.Append(imageClass.Replace(" ", "_"));
            thumb.Append("_");
            thumb.Append(id.ToString());
            thumb.Append((char)(34));
            thumb.Append(" alt=");
            thumb.Append((char)(34));
            thumb.Append(helper.Encode(title));
            thumb.Append(" image\" ");
            thumb.Append(" class=");
            thumb.Append((char)(34));
            thumb.Append(imageClass);
            thumb.Append((char)(34));
            thumb.Append("/></a>");
            thumb.Append("</div>");
            return thumb.ToString();

        }

        public static string CharityLogoLink(this HtmlHelper htmlHelper,string charityName, string charityLogo, string tagLine)
        {

            return ImageLink(htmlHelper,
                             new
                                 {
                                     title = "#" + PermaLinks.CreatePermaLinkPhrase(charityName),
                                     action = "charities",
                                     controller = "about"
                                 }, tagLine, charityLogo, 1, "supporting_logo");
               
        }

        
       public static string ProjectThumbNailLink(this HtmlHelper helper, int projectId, string projectTitle, string photoUrl)
       {
           string defaulCssClassForProjectPhotos = "project-photo";
           return ProjectThumbNailLink(helper, projectId, projectTitle, photoUrl, defaulCssClassForProjectPhotos);
       }

        public static string GenerateCaptcha( this HtmlHelper helper )  
{
    if (ConfigurationManager.AppSettings["reCAPTCHA_On"] == "false") return "";

    //http://rickardandersson.com/recaptcha-and-xhtml  
    var captchaControl = new Recaptcha.RecaptchaControl  
            {  
                    ID = "recaptcha",
                    Theme = ConfigurationManager.AppSettings["reCAPTCHA_Theme"],
                    PublicKey = ConfigurationManager.AppSettings["reCAPTCHA_PublicKey"],
                        PrivateKey = ConfigurationManager.AppSettings["reCAPTCHA_PrivateKey"],
                        
                        
        };  
  
    var htmlWriter = new HtmlTextWriter( new StringWriter() );  
  
    captchaControl.RenderControl(htmlWriter);  
  
    return htmlWriter.InnerWriter.ToString();  
}  

     public static string ScriptForTabFocus(this HtmlHelper htmlHelper, string tabSelected)
{
    StringBuilder focus = new StringBuilder(200);
    focus.Append(OpenJavaScriptTag());
         //refactor if more of this used.
         focus.Append("$(document).ready(function() {");
         focus.Append("$(");
         focus.Append((char)34);
         focus.Append(tabSelected);
         focus.Append((char)34);
         focus.Append(").addClass(");
         focus.Append((char)34);
         focus.Append("tab-focus");
         focus.Append((char)34);
         focus.Append(").show(");
         focus.Append((char)34);
         focus.Append("slow");
         focus.Append((char)34);
         focus.Append(");");
         focus.Append("});");
         focus.Append("</script>");
         return focus.ToString();
}

        private static string OpenJavaScriptTag()
        {
            StringBuilder script = new StringBuilder(200);
            script.Append("<script type=");
            script.Append((char)34);
            script.Append("text/javascript");
            script.Append((char)34);
            script.Append(">");
            return script.ToString();

        }

        public static string InputWithAutoText(this HtmlHelper htmlHelper, string name, int tabIndex, int maxLength, int size)
        {
            StringBuilder script = new StringBuilder(300);
            script.Append("<input name=");
            script.Append((char)34);
            script.Append(name);
            script.Append((char)34);
            script.Append(" id=");
            script.Append((char)34);
            script.Append(name);
            script.Append((char)34);
            script.Append(" tabindex=");
            script.Append((char)34);
            script.Append(tabIndex);
            script.Append((char)34);
            script.Append(" onfocus=");
            script.Append((char)34);
            script.Append("if (this.value=='");
            script.Append(name);
            script.Append("') this.value = ''");
            script.Append((char)34);
            script.Append(" onblur=");
            script.Append((char)34);
            script.Append("if (this.value=='') this.value = '");
            script.Append(name);
            script.Append("'");
            script.Append((char)34);
            script.Append(" type=");
            script.Append((char)34);
            script.Append("text");
            script.Append((char)34);
            script.Append(" maxlength=");
            script.Append((char)34);
            script.Append(maxLength.ToString());
            script.Append((char)34);
            script.Append(" size=");
            script.Append((char)34);
            script.Append(size.ToString());
            script.Append((char)34);
            script.Append(" value=");
            script.Append((char)34);
            script.Append(name);
            script.Append((char)34);
            script.Append(" />");
            return script.ToString();
        }

        public static string WarningMessage(this HtmlHelper htmlHelper, string message)
        {
            if(string.IsNullOrEmpty(message)) return "";
            StringBuilder html = new StringBuilder(100);
            html.Append("<div class=");
            html.Append((char)34);
            html.Append("warning");
            html.Append((char)34);
            html.Append(">");
            html.Append(htmlHelper.Encode(message));
            html.Append("</div>");
            return html.ToString();	
        }

        
    }


}
